<template>
  <div class="sales-statistics">
    <el-row :gutter="20" class="stats-row">
      <el-col :span="6">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-value">¥128,560</div>
            <div class="stat-label">今日销售额</div>
            <div class="stat-trend positive">↑ 12.5%</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-value">1,248</div>
            <div class="stat-label">今日订单数</div>
            <div class="stat-trend positive">↑ 8.3%</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-value">86</div>
            <div class="stat-label">今日客流量</div>
            <div class="stat-trend negative">↓ 2.1%</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-value">4.2</div>
            <div class="stat-label">客单价</div>
            <div class="stat-trend positive">↑ 5.7%</div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    
    <el-row :gutter="20" class="chart-row">
      <el-col :span="16">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>销售趋势</span>
            </div>
          </template>
          <div class="chart-container">
            <div ref="salesChartRef" style="width: 100%; height: 400px;"></div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>热门菜品排行</span>
            </div>
          </template>
          <div class="ranking-container">
            <el-table :data="dishRanking" style="width: 100%">
              <el-table-column prop="rank" label="排名" width="60" />
              <el-table-column prop="name" label="菜品名称" />
              <el-table-column prop="count" label="销售数量" width="100" />
              <el-table-column prop="amount" label="销售额" width="100" />
            </el-table>
          </div>
        </el-card>
      </el-col>
    </el-row>
    
    <el-row :gutter="20" class="chart-row">
      <el-col :span="12">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>分类销售占比</span>
            </div>
          </template>
          <div class="chart-container">
            <div ref="categoryChartRef" style="width: 100%; height: 400px;"></div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>时段销售分析</span>
            </div>
          </template>
          <div class="chart-container">
            <div ref="timeChartRef" style="width: 100%; height: 400px;"></div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue'
import * as echarts from 'echarts'

// 模拟热门菜品排行数据
const dishRanking = ref([
  { rank: 1, name: '宫保鸡丁', count: 128, amount: 3840 },
  { rank: 2, name: '麻婆豆腐', count: 96, amount: 1920 },
  { rank: 3, name: '北京烤鸭', count: 64, amount: 6400 },
  { rank: 4, name: '西湖醋鱼', count: 48, amount: 2400 },
  { rank: 5, name: '东坡肉', count: 42, amount: 1260 }
])

// 图表引用
const salesChartRef = ref(null)
const categoryChartRef = ref(null)
const timeChartRef = ref(null)

let salesChart = null
let categoryChart = null
let timeChart = null

// 初始化销售趋势图
const initSalesChart = () => {
  salesChart = echarts.init(salesChartRef.value)
  
  const option = {
    title: {
      text: '近7日销售趋势'
    },
    tooltip: {
      trigger: 'axis'
    },
    xAxis: {
      type: 'category',
      data: ['10-26', '10-27', '10-28', '10-29', '10-30', '10-31', '11-01']
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        data: [12000, 18000, 15000, 22000, 19000, 25000, 28000],
        type: 'line',
        smooth: true,
        areaStyle: {}
      }
    ]
  }
  
  salesChart.setOption(option)
}

// 初始化分类销售占比图
const initCategoryChart = () => {
  categoryChart = echarts.init(categoryChartRef.value)
  
  const option = {
    title: {
      text: '分类销售占比',
      left: 'center'
    },
    tooltip: {
      trigger: 'item'
    },
    legend: {
      orient: 'vertical',
      left: 'left'
    },
    series: [
      {
        type: 'pie',
        radius: '50%',
        data: [
          { value: 35, name: '川菜' },
          { value: 25, name: '粤菜' },
          { value: 20, name: '湘菜' },
          { value: 15, name: '鲁菜' },
          { value: 5, name: '其他' }
        ],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  }
  
  categoryChart.setOption(option)
}

// 初始化时段销售分析图
const initTimeChart = () => {
  timeChart = echarts.init(timeChartRef.value)
  
  const option = {
    title: {
      text: '时段销售分析'
    },
    tooltip: {
      trigger: 'axis'
    },
    xAxis: {
      type: 'category',
      data: ['08-10', '10-12', '12-14', '14-16', '16-18', '18-20', '20-22']
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        data: [120, 350, 800, 650, 420, 980, 720],
        type: 'bar'
      }
    ]
  }
  
  timeChart.setOption(option)
}

// 窗口大小改变时重置图表大小
const resizeCharts = () => {
  if (salesChart) salesChart.resize()
  if (categoryChart) categoryChart.resize()
  if (timeChart) timeChart.resize()
}

// 组件挂载时初始化图表
onMounted(() => {
  initSalesChart()
  initCategoryChart()
  initTimeChart()
  
  window.addEventListener('resize', resizeCharts)
})

// 组件卸载前清理事件监听器
onBeforeUnmount(() => {
  window.removeEventListener('resize', resizeCharts)
  
  if (salesChart) salesChart.dispose()
  if (categoryChart) categoryChart.dispose()
  if (timeChart) timeChart.dispose()
})
</script>

<style scoped>
.stats-row {
  margin-bottom: 20px;
}

.stat-card {
  text-align: center;
}

.stat-content {
  padding: 20px 0;
}

.stat-value {
  font-size: 24px;
  font-weight: bold;
  color: #409eff;
  margin-bottom: 10px;
}

.stat-label {
  font-size: 14px;
  color: #666;
  margin-bottom: 10px;
}

.stat-trend {
  font-size: 12px;
}

.stat-trend.positive {
  color: #67c23a;
}

.stat-trend.negative {
  color: #f56c6c;
}

.card-header {
  font-weight: bold;
}

.chart-row {
  margin-bottom: 20px;
}

.chart-container {
  height: 400px;
}
</style>